package com.vp.org.dao.impl;

import com.vp.org.constant.Sql;
import com.vp.org.dao.IntegralExchangeDao;
import com.vp.org.pojo.IntegralExchange;
import com.vp.org.util.JdbcUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class IntegralExchangeDaoImpl extends JdbcUtil implements Sql, IntegralExchangeDao {

    // 上架可兑换商品（增加）
    // insert into integral_exchange values (default,?,?,?,?);
    @Override
    public int add(IntegralExchange integralExchange) {
        return template.update(INTEGRAL_ADD,integralExchange.getIntegral(),integralExchange.getAdmin(),
                integralExchange.getGoodsName(),integralExchange.getGoodsType());
    }

    // 下架可兑换商品（删除）
    // delete from integral_exchange where id = ?;
    @Override
    public int delete(IntegralExchange integralExchange) {
        return template.update(INTEGRAL_DELETE,integralExchange.getId());
    }

    // 修改可兑换商品
    // update integral_exchange set integral = ?,  admin    = ?,goods_name = ?,goods_type = ?where id = ?;
    @Override
    public int upadte(IntegralExchange integralExchange) {
        return template.update(INTEGRAL_UPDATE,integralExchange.getIntegral(),integralExchange.getAdmin(),
                integralExchange.getGoodsName(),integralExchange.getGoodsType(),integralExchange.getId());
    }

    // 查询所有可兑换商品总数
    // select count(id) count from integral_exchange;
    @Override
    public long count() {
        return (long)template.queryForMap(INTEGRAL_COUNT).get("count");
    }

    // 所有可兑换商品总页数
    @Override
    public int exchangePage() {
        int count = (int) count();
        return count % 4 == 0 ? count / 4 : count / 4 + 1;
    }

    // 分页查询可兑换商品
    // select * from integral_exchange limit 0,4;
    @Override
    public List<IntegralExchange> queryPage(int page) {
        return template.query(INTEGRAL_QUERYPAGE,new BeanPropertyRowMapper<>(IntegralExchange.class),(page - 1) * 4);
    }
}
